package com.yorge.controller;

import com.yorge.entities.CommonResult;
import com.yorge.entities.Payment;
import com.yorge.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/getPayment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable Long id) {
        Payment payment = paymentService.getPayment(id);
        CommonResult<Payment> result = CommonResult.success("success:" + port);
        result.setData(payment);

        return result;
    }

    @PostMapping("/addPayment")
    public CommonResult<String> addPayment(@RequestBody Payment payment) {
        int res = paymentService.addPayment(payment);
        if (res > 0) {
            return CommonResult.success("success:" + port);
        } else {
            return CommonResult.error("添加失败");
        }
    }

    @RequestMapping("/timeOut")
    public String timeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        return port;
    }

    @RequestMapping("/get/getPort")
    public String getPort() {
        return port;
    }
}
